// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var Shares = require('./gameshare');
var SceneTypes = Shares.SceneTypes;
var GameEvent = Shares.GameEvent;
var ShaderUtils = require('./utils/shader_tools');
cc.Class({
    extends: cc.Component,

    properties: {
        editNode: cc.Node,
        titleNode: cc.Node,
        winNode: cc.Node,
        maxMaps: 6
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        cc.zsb = {};
        cc.zsb.sceneType = SceneTypes.SceneTitle;
        cc.zsb.selectNode = null;
        var map = cc.sys.localStorage.getItem('gamemap')
        console.log(map);
        if(map == null){
            map = 1;
        }
        if(map > this.maxMaps){
            map = 1;
        }
        console.log(map);
        cc.zsb.mapIdx = map || 1;
        cc.systemEvent.on(GameEvent.GameWin, this.onGameWin, this);
    },

    start() {
        this.winNode.active = false;
        this.titleNode.active = true;
        //cc.systemEvent.emit(GameEvent.LoadMap, "title");
        this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
            if(cc.zsb.sceneType == SceneTypes.SceneGame){
                cc.systemEvent.emit(GameEvent.GameTouched);
            }        
        }, this);
    },

    enterEdit(){
        cc.zsb.sceneType = SceneTypes.SceneEdit;
        this.titleNode.active = false;
        this.editNode.active = true;
        cc.systemEvent.emit(GameEvent.EnterEdit);
    },

    enterNextMap(){
        var mapid = cc.zsb.mapIdx;
        console.log(mapid);
        cc.zsb.sceneType = SceneTypes.SceneGame;
        this.titleNode.active = false;
        this.editNode.active = false;
        this.winNode.active = false;
        cc.systemEvent.emit(GameEvent.LoadMap, 'map_' + mapid);
    },

    onGameWin(){
        cc.zsb.player.isPause = true;
        this.winNode.active = true;
        cc.zsb.mapIdx = cc.zsb.mapIdx + 1;
        if(cc.zsb.mapIdx > this.maxMaps){
            cc.zsb.mapIdx = 1;
        }
    },
});
